package com.book.dao.Impl;

import com.book.dao.bookDao;
import com.book.pojo.Book;
import com.book.pojo.Page;

import java.util.List;

public class bookDaoImpl extends baseDao implements bookDao {

    @Override
    public int addBook(Book book) {
        String sql = "insert into t_book values(?,?,?,?,?,?,?)";
        return update(sql, book.getId(), book.getName(), book.getPrice(), book.getAuthor(), book.getSales(), book.getStock(), book.getImgPath());
    }

    //(id,name,price,author,sales,stock,img_path)
    @Override
    public int updateBook(Book book) {
        String sql = "update t_book set id=?,name=?,price=?,author=?,sales=?,stock=?,img_path=? where id=?";
        return update(sql, book.getId(), book.getName(), book.getPrice(), book.getAuthor(), book.getSales(), book.getStock(), book.getImgPath(),book.getId());
    }

    @Override
    public int delBookById(Integer id) {
        String sql = "delete from t_book where id =?";
        return update(sql, id);
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql = "select * from t_book where id =?";
        return queryForOne(Book.class,sql, id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql = "select * from t_book";
        return queryForList(Book.class,sql);
    }


    @Override
    public Integer queryForPageCount() {
        String sql = "select count(*) from t_book";
        Number count = (Number) queryCount(sql);
        return count.intValue();
    }

    @Override
    public List<Book> queryForPageItems(int begin, int pageSize) {
        String sql = "select * from t_book limit ?,?";
        return queryForList(Book.class, sql, begin,pageSize);
    }
}
